package com.example.xquicdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.Exception

class TextOkhttpActivity : AppCompatActivity() {
    var clinet:OkHttpClient=OkHttpClient()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_text_okhttp)
        GlobalScope.launch {
            try {
                testOkhttp()
            }catch (e:Exception){
                e.printStackTrace()
            }
        }
    }

    /**
     * 测试https
     */
    fun testOkhttp(){
        var startTime=System.currentTimeMillis()
        val url = SetCache.getSelectUrl(applicationContext)
        if (url.isNullOrEmpty()) {
            Toast.makeText(applicationContext, "请先设置url", Toast.LENGTH_SHORT).show()
            return
        }
        var request =  Request.Builder()
            .url(url)
            .build();
        var response=clinet.newCall(request).execute()
        var text=response.body?.string()
        System.out.println(text)
        System.out.print("=>>"+(System.currentTimeMillis()-startTime))
    }
}